<?php
/**
 * 订单商品库存占用明细
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/3/25
 * Time: 14:46
 */
namespace App\Http\Controllers\WarehouseManage;
use App\Http\Controllers\ApiController;
use App\Jobs\AutoOrderCancelQueue;
use App\Services\Orders\OrderService;
use App\Services\Warehouse\OrderGoodsInventoryCarryingDetailService;
use Illuminate\Http\Request;
use PHPUnit\Runner\Exception;

class OrderGoodsInventoryCarryingDetailController extends ApiController{
    protected $OrderGoodsInventoryCarryingDetailService;
    protected $OrderService;

    public function __construct(OrderGoodsInventoryCarryingDetailService $orderGoodsInventoryCarryingDetailService,OrderService $orderService)
    {
        $this->OrderGoodsInventoryCarryingDetailService=$orderGoodsInventoryCarryingDetailService;
        $this->OrderService=$orderService;
    }

    /***
     * 提交占用订单商品库存请求
     * 参数说明
     * orderdata={"order_id":1833,"order_sn":"2016082215562302883","wid":8,"GoodsData":[{"goods_id":989,"goods_sn":"ECS018343","goods_number":3,"k3_supplier_sn":"","k3_vmi":0,"spec_unit":"\u76d2","best_time":1521961937}]}
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function SubmitOrderGoodsInventoryRequest(Request $request){
        $order=$request->orderdata;
        if(empty($order)){
            return $this->failed('非法请求');
        }
        $data=json_decode($order,true);
        if(empty($data)){
            return $this->failed('参数错误');
        }
        if(empty($data['freight'])){
            $data['freight']=0;
        }
        if(empty($data['discountamount'])){
            $data['discountamount']=0;
        }
        $orderdata=[
            'order_id'=>$data['order_id'],
            'order_sn'=>$data['order_sn'],
            'wid'=>$data['wid'],
            'freight'=>$data['freight'],
            "discountamount"=>$data['discountamount'],
            'GoodsData'=>$data['GoodsData'],
        ];
        try {
            $return=$this->OrderGoodsInventoryCarryingDetailService->SubmitOrderGoodsInventoryRequest($orderdata);
            if($return){
                AutoOrderCancelQueue::dispatch($data['order_id'])
                    ->onQueue('AutoOrderCancelQueue')
                    ->delay(now()->addMinutes(config('shidexian.auto_order_cancel_time')));
                return $this->success($return);
            }else{
                return $this->failed('订单商品库存占用失败');
            }
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }

    }

    /***
     * 订单自动取消
     * 参数说明
     * order_id
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function AutoOrderCancel(Request $request){
        $order_id=$request->order_id;
        try{
            AutoOrderCancelQueue::dispatch($order_id)
                ->onQueue('AutoOrderCancelQueue')
                ->delay(now()->addMinutes(config('shidexian.auto_order_cancel_time')));
            return $this->success(true);
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }

    }

    /***
     * 用户手动取消订单
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function UsersHandOrderCancel(Request $request){
        $order_id=$request->order_id;
        $user_id=$request->user_id;
        if(empty($order_id) || empty($user_id)){
            return $this->failed('非法操作');
        }
        try{
            $return=$this->OrderService->ExecuteUsersHandOrderCancel($order_id,$user_id);
            if($return){
                return $this->success($return);
            }else{
                return $this->failed('操作失败');
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }
    }

}